﻿using System;
using System.IO;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace HelloMVC.Service.Infrastructure
{
    public class JsonFilter : ActionFilterAttribute
    {
        public string Param { get; set; }
        public Type JsonDataType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
            {
                var objResult = JsonConvert.DeserializeObject(new StreamReader(filterContext.HttpContext.Request.InputStream).ReadToEnd(), JsonDataType);
                filterContext.ActionParameters[Param] = objResult;
            }
        }
    }
}
